//
//  Assetsmanager.m
//  YCSchool
//
//  Created by yanglinxia on 16/5/3.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//

#import "YCAssetsManager.h"

@interface YCAssetsManager ()

///相册库
@property (nonatomic,strong) ALAssetsLibrary *assetsLibrary;

@end

@implementation YCAssetsManager
@synthesize assetsGroups = _assetsGroups;
@synthesize defaultAlbumGroup = _defaultAlbumGroup;
// 单例
+ (instancetype)shareManager
{
    static YCAssetsManager *_shareManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _shareManager = [[self alloc] init];
    });
    return _shareManager;

}
//相册库
- (ALAssetsLibrary *)assetsLibrary
{
    if(!_assetsLibrary)
    {
        _assetsLibrary = [[ALAssetsLibrary alloc] init];
    }
    return _assetsLibrary;
}
///系统相册默认分组
- (ALAssetsGroup *)defaultAlbumGroup
{
    return self.assetsGroups.firstObject;
}

// 加载相册
- (void)loadAllAssetsGroupsCompletionBlock:(AssetsManagerLoadAlbumCompletionBlock)block
{
    if(self.assetsGroups)
    {
        block(self);
    }
    else
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSMutableArray *groups = [[NSMutableArray alloc] init];
            
            [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
                if(group && group.numberOfAssets > 0)
                {
                if ([[group valueForProperty:ALAssetsGroupPropertyType] intValue]==ALAssetsGroupSavedPhotos){
                        [groups insertObject:group atIndex:0];//所有相片
                    } else if ([[group valueForProperty:ALAssetsGroupPropertyType] intValue]==ALAssetsGroupPhotoStream && groups.count>0){
                        [groups insertObject:group atIndex:1];//照片流
                    } else {
                        [groups addObject:group];//其它相片组
                    }
                    
                    _assetsGroups = [[NSArray alloc] initWithArray:groups];//给所有相册数组赋值
                    
//                    NSString *a = [group valueForProperty:ALAssetsGroupPropertyName];
//                    if([a isEqualToString:@"相机胶卷"])
//                    {
//                        [groups insertObject:group atIndex:0];
//                    }
//                    else
//                    {
//                        [groups addObject:group];
//                    }
                }
                else
                {
                    _assetsGroups = [[NSArray alloc] initWithArray:groups];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        block(self);
                    });
                }
            } failureBlock:^(NSError *error) {
                //没权限
                [self showNotAllowed];
            }];
        });
    }
}

#pragma mark - Not allowed / No assets
- (void)showNotAllowed {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NotAllowedPhoto" object:nil];
}


@end
